package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IFastArticleTypeService;
import com.mdd.admin.validate.FastArticleTypeCreateValidate;
import com.mdd.admin.validate.FastArticleTypeUpdateValidate;
import com.mdd.admin.validate.FastArticleTypeSearchValidate;
import com.mdd.admin.vo.FastArticleTypeListedVo;
import com.mdd.admin.vo.FastArticleTypeDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.FastArticleType;
import com.mdd.common.mapper.FastArticleTypeMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 快递物品分类配置实现类
 * @author DEREK
 */
@Service
public class FastArticleTypeServiceImpl implements IFastArticleTypeService {
        
    @Resource
    FastArticleTypeMapper fastArticleTypeMapper;

    /**
     * 快递物品分类配置列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<FastArticleTypeListedVo>
     */
    @Override
    public PageResult<FastArticleTypeListedVo> list(PageValidate pageValidate, FastArticleTypeSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<FastArticleType> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        fastArticleTypeMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
            "=:fid:int",
            "=:remark:str",
            "=:extend:str",
        });

        IPage<FastArticleType> iPage = fastArticleTypeMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<FastArticleTypeListedVo> list = new LinkedList<>();
        for(FastArticleType item : iPage.getRecords()) {
            FastArticleTypeListedVo vo = new FastArticleTypeListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 快递物品分类配置详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return FastArticleType
     */
    @Override
    public FastArticleTypeDetailVo detail(Integer id) {
        FastArticleType model = fastArticleTypeMapper.selectOne(
                new QueryWrapper<FastArticleType>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        FastArticleTypeDetailVo vo = new FastArticleTypeDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 快递物品分类配置新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(FastArticleTypeCreateValidate createValidate) {
        FastArticleType model = new FastArticleType();
        model.setName(createValidate.getName());
        model.setFid(createValidate.getFid());
        model.setRemark(createValidate.getRemark());
        model.setExtend(createValidate.getExtend());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        fastArticleTypeMapper.insert(model);
    }

    /**
     * 快递物品分类配置编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(FastArticleTypeUpdateValidate updateValidate) {
        FastArticleType model = fastArticleTypeMapper.selectOne(
                new QueryWrapper<FastArticleType>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setFid(updateValidate.getFid());
        model.setRemark(updateValidate.getRemark());
        model.setExtend(updateValidate.getExtend());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        fastArticleTypeMapper.updateById(model);
    }

    /**
     * 快递物品分类配置删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        FastArticleType model = fastArticleTypeMapper.selectOne(
                new QueryWrapper<FastArticleType>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        fastArticleTypeMapper.delete(new QueryWrapper<FastArticleType>().eq("id", id));
    }

}
